package com.hzf.material.rpc.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.wenhao.geohash.domain.GeoRange;
import com.hzf.common.annotation.BaseService;
import com.hzf.common.base.BaseServiceImpl;
import com.hzf.common.db.DataSourceEnum;
import com.hzf.common.db.DynamicDataSource;
import com.hzf.material.dao.Generator;
import com.hzf.material.dao.mapper.MaterialGoodsMapper;
import com.hzf.material.dao.model.MaterialGoods;
import com.hzf.material.dao.model.MaterialGoodsExample;
import com.hzf.material.rpc.api.MaterialGoodsService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* MaterialGoodsService实现
* Created by shuhzf on 2017/11/20.
*/
@Service
@Transactional
@BaseService
public class MaterialGoodsServiceImpl extends BaseServiceImpl<MaterialGoodsMapper, MaterialGoods, MaterialGoodsExample> implements MaterialGoodsService {

    private static Logger _log = LoggerFactory.getLogger(MaterialGoodsServiceImpl.class);

    @Autowired
    MaterialGoodsMapper materialGoodsMapper;

    public List<MaterialGoods> selectByPrimaryCity(MaterialGoodsExample  example,int offset,int limit){
/*        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        Method selectByExample = materialGoodsMapper.getClass().getDeclaredMethod("selectByExample", example.getClass());
        PageHelper.offsetPage(offset, limit, false);
        Object result = selectByExample.invoke(mapper, example);

        PageHelper.offsetPage(offset, limit, false);

        return  materialGoodsMapper.selectByPrimaryCity(example,offset,limit);*/
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
            Method selectByExample = mapper.getClass().getDeclaredMethod("selectByPrimaryCity", example.getClass());
            PageHelper.offsetPage(offset, limit, false);
            Object result = selectByExample.invoke(mapper, example);
            return (List<MaterialGoods>) result;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        DynamicDataSource.clearDataSource();
        return null;
    }
    public int countByCityExample(MaterialGoodsExample materialGoodsExample){
        long count =  materialGoodsMapper.countByCityExample(materialGoodsExample);
        return Integer.parseInt(String.valueOf(count));
    }

/*    @Override
    public List<MaterialGoods> selectByPrimaryDistance(int offset,int limit, int regionNumber, int typeId, int status,
                                                       long min0, long max0,
                                                    long min1, long max1, long min2, long max2,
                                                      long min3, long max3,
                                                      long min4, long max4,
                                                       long min5, long max5,
                                                      long min6, long max6,
                                                      long min7, long max7,
                                                      long min8,long max8) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
            Map map = new HashMap();
            map.put("min0",min0);
            map.put("min1",min1);
            map.put("min2",min2);
            map.put("min3",min3);
            map.put("min4",min4);
            map.put("min5",min5);
            map.put("min6",min6);
            map.put("min7",min7);
            map.put("min8",min8);
            map.put("max0",max0);
            map.put("max1",max1);
            map.put("max2",max2);
            map.put("max3",max3);
            map.put("max4",max4);
            map.put("max5",max5);
            map.put("max6",max6);
            map.put("max7",max7);
            map.put("max8",max8);

            Method selectByExample = mapper.getClass().getDeclaredMethod("selectByPrimaryDistance", map.getClass());
            PageHelper.offsetPage(offset, limit, false);
            Object result = selectByExample.invoke(mapper, example);
            return (List<MaterialGoods>) result;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        DynamicDataSource.clearDataSource();
        return null;
*//*
        return materialGoodsMapper.selectByPrimaryDistance(regionNumber,typeId,status,min0,max0,min1,max1,min2,max2,min3,max3,min4,max4,min5,max5,min6,max6,min7,max7,min8,max8);
*//*
    }*/
}